var commLoadTimer=false;
function do_photo_pop(obj){
		if(commLoadTimer) clearTimeout(commLoadTimer);
		
	  var id=$(obj).attr('href').split('/').pop();
	  var elm=$(obj);
	  
     		var sw=$(window).width()-50;
     		var sh=$(window).height()-50;
     		if(sw>1200) sw=1200;
     		if(sh>900) sw=900;
     		sh=600;

	  if( $('#photo_pop_item').get().length<1 ){
			var pg_content='<div id="photo_pop_item" class="loading"><div class="load"></div></div>';
			$.fancybox(
				pg_content,
				{
					'padding'		: 0,
					'autoScale'		: false,
					'autoDimensions': false,
					'transitionIn'	: 'none',
					'transitionOut'	: 'none',
					'centerOnScroll':true,
					'titleShow':false,
					'scrolling':'no',
					'title'			: '',
					'width'		: sw,
					'height'		: sh,
					'keys':{
					  'next':null,
					  'prev':null,
					},
					'onComplete':function(){
					  $('#photo_pop_item .prev,#photo_pop_item .next').click(function(){
					    do_photo_pop(this); return false;
						});
					}
				}
			);
		}else{
		  $('#photo_pop_item').addClass('loading');
		  $('#photo_pop_item .ill').animate({opacity:0},600);
		}
	  
	  make_request(
			{
			  mode:'photo_item',
				photo_id:id
			},
			function (req){
     		var pg_content=req.responseJS.block.replace('{href}',elm.attr('href'));

				$.fancybox(
					pg_content,
					{
						'padding'		: 0,
						'autoScale'		: false,
						'autoDimensions': false,
						'transitionIn'	: 'none',
						'transitionOut'	: 'none',
						'centerOnScroll':true,
						'titleShow':false,
						'scrolling':'no',
						'title'			: '',
						'width'		: sw,
						'height'		: sh,
						'keys':{
						  'next':null,
						  'prev':null,
						},
						'onComplete':function(){
						  $('#photo_pop_item').removeClass('loading');
						  $('#photo_pop_item .prev,#photo_pop_item .next').click(function(){
						    do_photo_pop(this); return false;
							});
							var l1=1000, l2=2000;
							commLoadTimer=setTimeout(function(){
								$('#comms_block_pop').html('<div class="loading"></div>');
                commLoadTimer=setTimeout(function(){load_comments(id);},l2);
							},l1);
						}
					}
				);
			}
		);
}
var keys_arr=[];
function check_action(){
	if( $('#photo_pop_item').hasClass('loading') ) return false;
	if(keys_arr.length==1){
	  var key=keys_arr[0];
	}else{
		if(keys_arr[0]!=17) return false;
		var key=keys_arr[1];
	}
	
	//right
	if(key==39){
	  $('#photo_pop_item .next').click();
	}else
	//left
	if(key==37){
	  $('#photo_pop_item .prev').click();
	}
}

function load_comments(objID){
	  make_request(
			{
			  mode:'comments',
				id:objID
			},
			function (req){
     		var pg_content=req.responseJS.block;
     		$('#comms_block_pop').html(pg_content);
     		
     		if( $('#comms_block_pop .comments-list').get().length ){
	     		var th=$('#photo_pop_item').height() - $('#comms_block_pop').position().top - $('#comms_block_pop .comments-list').position().top;
	     		$('#comms_block_pop .comments-list').css({ height:th+'px', overflow:'auto' });
	     		$('#comms_block_pop .comments-list').scroll(function(){
	     		  $('#comms_block_pop .comments-list li').css('margin-bottom','5px');
	     		  $('#comms_block_pop .comment_add').css({'top':0}).removeClass('answer');
					});
					init_walls();
				}
     		
     		$('#comms_block_pop .comment_add form').submit(function(){
     		  send_comment( objID, this.ans.value, this.w_id.value , this.comment.value );
     		  return false;
				});
			}
		);
}

var sending_comment=false;
function send_comment( objID, ansID, wallID, commTXT ){
	if(sending_comment) return false;
	sending_comment=true;
	  $('#comms_block_pop .comment_add .comment').val('');
	  $('#comms_block_pop .comments-list').html('<li style="background:url(/img/loading_sm.gif) center center no-repeat;"></li>');
		make_request(
			{
			  mode:'comment_add',
				id:objID,
				ans:ansID,
				edt:wallID,
				txt:commTXT
			},
			function (req){
			  sending_comment=false;
			  load_comments(objID);
			}
		);
}

$(document).ready(function(){
	if( $('.photo_list').get().length<1 ) return;
	$('.photo_list .lnk').click(function(){
	  do_photo_pop(this); return false;
	});
	$( document ).keydown(function( event ) {
	  console.log(event);
    if( $('.comment').is(":focus") ){
      return true;
		}else{
	    console.log( 'down '+event.which );
			switch ( event.which ) {
			  case 17: //CTRL
			  case 39: //right
			  case 37: //left
			  keys_arr.push(event.which);
			  if(keys_arr.length>=1) check_action();
				event.preventDefault();
				return false;
			}
		}
	});
	$( document ).keyup(function( event ) {
	  if( $('.comment').is(":focus") ){
	    return true;
		}else{
			switch ( event.which ) {
			  case 17: //CTRL
			  case 39: //right
			  case 37: //left
			  keys_arr.pop();
				event.preventDefault();
				return false;
			}
		}
	});
});